//
//  TYZCheckButton.m
//  VehicleInsuranceSupport
//
//  Created by TangBin on 2019/11/25.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZCheckButton.h"

@implementation TYZCheckButton

- (void)dealloc
{
//    debugMethod();
}

// 点击事件
- (void)clicked:(id)sender
{
    if (_delegate != nil)
    {
        SEL sel = NSSelectorFromString(@"checkButtonClicked:");
        if ([_delegate respondsToSelector:sel])
        {
            [_delegate performSelector:sel withObject:sender];
        }
    }
    else
    {
        [self setChecked:!self.checked];
    }
}

//初始化
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        _checkname = @"";
        _uncheckname = @"";
        _icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, frame.size.height, frame.size.height)];
        [_icon setContentMode:UIViewContentModeScaleToFill];
        [self setStyle:CheckButtonStyleDefault];//默认风格为方框(多选)样式
        [self addSubview:self.icon];
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(self.icon.frame.size.width+10, 0, frame.size.width-self.icon.frame.origin.x-self.icon.frame.size.width-5-10, frame.size.height)];
        self.label.backgroundColor = [UIColor clearColor];
        self.label.font = [UIFont fontWithName:@"Arial" size:14];
        self.label.textAlignment=NSTextAlignmentLeft;
//        _label.backgroundColor = [UIColor redColor];
        [self addSubview:self.label];
        [self addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

- (CheckButtonStyle)_style
{
    return _style;
}

- (void)setStyle:(CheckButtonStyle)st
{
    
    _style = st;
    switch (self.style)
    {
        case CheckButtonStyleDefault:
            
        case CheckButtonStyleBox:
            self.checkname = @"checked.png";
            self.uncheckname = @"unchecked.png";
            break;
        case CheckButtonStyleRadio:
            self.checkname = @"ic_radio_check";
            self.uncheckname = @"ic_radio_uncheck";
            break;
        default:
            break;
    }
    [self setChecked:_checked];
}

- (BOOL)_checked
{
    return _checked;
}

- (void)setChecked:(BOOL)ck
{
    if (ck != _checked)
    {
        _checked = ck;
    }
    if (_checked)
    {
        [self.icon setImage:[UIImage imageNamed:self.checkname]];
        if (_delegate!=nil && _isClicked)
        {//如果使用了radiogroup，把选中值同步到radiogroup
//            NSLog(@"delegate is not nil");
            SEL sel = NSSelectorFromString(@"selectdValue:");
            NSDictionary *dict = @{@"text":_label.text, @"index":_value};
//            NSLog(@"text=%@; index=%@", _label.text, _value);
            if ([_delegate respondsToSelector:sel])
            {
                [_delegate performSelector:sel withObject:dict];
            }
        }
    }
    else
    {
//        NSLog(@"else");
        [self.icon setImage:[UIImage imageNamed:self.uncheckname]];
    }
}
@end
